package com.qf.web;


import com.qf.pojo.Admin;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决中文乱码
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        UserService userService=new UserServiceImpl();

        //获取前端页面输入的值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Admin admin = new Admin(username,password);
        //调用登录方法
        List<Admin> list = userService.login(admin);
        List<Admin> all = userService.findAll();
        //数据存到session域中
        HttpSession session = req.getSession();

        if (list.size()>0&&list!=null){
            session.setAttribute("user",list);
            if (all.size()>0&&all!=null){
                session.setAttribute("admins",all);
            }
            req.getRequestDispatcher("/el.jsp").forward(req,resp);
        }else{
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }
    }
}
